package spring.boot.es.config;


import org.apache.http.HttpHost;
import org.elasticsearch.client.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author liuli
 * https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/7.17/connecting.html
 *
 * https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/7.17/api-conventions.html
 */

@Configuration
public class ElasticsearchRestClient {
    private static final Logger log = LoggerFactory.getLogger(ElasticsearchRestClient.class);
    @Value("${grayscale.elasticsearch.cluster-nodes}")
    private String clusterNodes ;


    @Bean(name = "restClient")
    public RestClient  restClient() {
        HttpHost[]  httpHosts = new HttpHost[clusterNodes.split(",").length] ;
        for (int i = 0; i < clusterNodes.split(",").length; i++) {
            String nodes = clusterNodes.split(",")[i];
            String InetSocket [] = nodes.split(":");
            String  host = InetSocket[0];
            Integer  port = Integer.valueOf(InetSocket[1]);
            httpHosts[i]= new HttpHost(host, port);
        }
        // Create the low-level client
        return  RestClient.builder(httpHosts).build();
    }




}